package com.person.news.service.impl;

import java.util.List;

import com.person.commons.utils.DAO;
import com.person.commons.utils.FlipPageInfo;
import com.person.news.model.News;
import com.person.news.model.NewsAnnex;
import com.person.news.service.NewsService;
import com.person.title.model.TitleFunc;

public class NewsServiceImpl implements NewsService {

	private DAO dao;

	@Override
	public void createNewsAnnex(NewsAnnex newsAnnex) {
		dao.save(newsAnnex);
	}

	public void setDao(DAO dao) {
		this.dao = dao;
	}

	@Override
	public String createNews(News news) throws Exception {
		// TODO Auto-generated method stub
		return (String) dao.save(news);
	}

	@Override
	public FlipPageInfo findAllNews(FlipPageInfo fpi) {
		return dao
				.findWithPaging(
						"select n.newsType,n.userDate,n.newsTitle,u.userName,n.newsId,n.newsFlag," +
						"n.newsTop,n.newsClick,t.funcName from News n,BaseUser u,TitleFunc t " +
						"where n.userId = u.userId and t.funcId=n.funcId order by n.funcId,n.newsType,n.userDate desc",
						fpi);

	}

	@Override
	public News findNewsById(String id) {
		// TODO Auto-generated method stub
		try {
			News news = (News) dao.find("from News where newsId='" + id + "'")
					.get(0);
			return news;
		} catch (RuntimeException e) {
			return new News();
		}
	}

	@Override
	public List<NewsAnnex> findAnnexByNewsId(String id) {
		// TODO Auto-generated method stub
		return dao.find("from NewsAnnex where newsId='" + id + "'");
	}

	@Override
	public void deleteNewsById(String id) {
		// TODO Auto-generated method stub
		dao.delete(findNewsById(id));
		if (findAnnexByNewsId(id) != null && findAnnexByNewsId(id).size() > 0) {
			for (NewsAnnex n : findAnnexByNewsId(id)) {
				dao.delete(n);
			}
		}
	}

	@Override
	public void deleteNewsAnnex(String[] ids) {
		// TODO Auto-generated method stub
		if (ids != null) {
			for (String id : ids) {
				if (dao.find("from NewsAnnex where annexId='" + id + "'")
						.get(0) != null) {
					dao.delete(dao.find("from NewsAnnex where annexId='"+ id + "'").get(0));
				}
			}
		}
	}

	@Override
	public void modifyNews(News news) {
		// TODO Auto-generated method stub
		dao.update(news);
	}

	@Override
	public FlipPageInfo findNewsByQualification(String hql, FlipPageInfo fpi) {
		// TODO Auto-generated method stub

		try{
			return dao.findWithPaging(hql, fpi);
		}catch(Exception e){
			System.out.println("查询新闻列表出现异常");
		}
		return new FlipPageInfo();
		
	}

	@Override
	public boolean findNewsByNewsTitle(String newsTitle) {
		List l = dao.find("from News where newsTitle='"+newsTitle+"'");
		if(l.size()!=0){
			return false;
		}else{
			return true;
		}
	}
	/**
	 * 根据funcId从栏目功能表TitleFunc查找功能名
	 */
	public TitleFunc findTitleFuncByFuncId(String funcId){
		try{
			return (TitleFunc) dao.find(TitleFunc.class, funcId);
		}catch(Exception e){
			System.out.println("栏目功能表TitleFunc查找功能名出现异常");
		}
		return new TitleFunc();
	}
	
	/**
	 * 根据funcId查找News
	 */
	public List<News> findNewsByFuncId(String funcId) {
		return this.dao.find("from News where funcId='"+funcId+"' and newsFlag='0' and newsTop = '1' order by userDate desc") ;
	}
	/**
	 * 查找产品信息，分页列表
	 * @param fpi
	 * @return
	 */
	public FlipPageInfo findNewsByFuncId (String funcId ,FlipPageInfo fpi) {
		return this.dao.findWithPaging("from News where funcId='"+funcId+"' and newsFlag='0' order by newsTop desc , userDate desc", fpi) ;
	}	
	public List<News> findNewsByNewsFlag(int newsFlag){
		
		return dao.find("from News where newsRevert="+newsFlag+" and  newsFlag='0' order by userDate desc");
	}

}
